<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>formdata</title>
  <script>
    window.onload = function () {
      let oform = document.querySelector('#form1');
      oform.onsubmit = function () {
        let XHR = new XMLHttpRequest();

        let _formData = new FormData(oform);
        XHR.open(this.method, this.action, true);
        XHR.send(_formData);
        XHR.onreadystatechange = function () {
          if (XHR.readyState == 4) {
            if (XHR.status >= 200 & XHR.status < 300 || XHR.status == 304) {
              console.log('成功了')

            }
          }
        }
        return false;
      }

      let user = document.querySelector('#user');
      let password = document.querySelector('#password');
      let fl = document.querySelector('#files');
      let btn1 = document.querySelector('#btn1');

      btn1.onclick = function () {
        console.log('object');
        let XHR = new XMLHttpRequest();
        let formdata = new FormData();
        formdata.append('username', user.value);
        formdata.append('password', password.value);
        formdata.append('fl', fl.files[0]);

        XHR.open('post', 'http://localhost:9895/aaa', true)
        console.log('formdata', formdata);

        XHR.send(formdata);
        XHR.onreadystatechange = function () {
          if (XHR.readyState == 4) {
            if (XHR.status >= 200 & XHR.status < 300 || XHR.status == 304) {
              console.log('成功了')

            }
          }
        }

      }
    }

  </script>
</head>

<body>
  <div>
    <form action="http://localhost:9894/aaa" method='post' id="form1">
      用户名<input type="text" name="user"><br>
      密码<input type="password" name="password"><br>
      文件上传<input type="file" name="fl"><br>
      <input type="submit" value="提交">
    </form>
  </div>

  <h3>脱离form表单写 formdata</h3>
  <div>

    用户名<input type="text" id="user"><br>
    密码<input type="password" id="password"><br>
    文件上传<input type="file" id="files"><br>
    <input type="button" value="提交" id="btn1">

  </div>



</body>

</html>